package com.zxx.myapplication;

import android.annotation.SuppressLint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

/**
 * 甩歌应用
 */
public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private TextView textView1, textView2, textView3;

    private SensorManager sensorManager;
    private Sensor sensor;

    private MediaPlayer mediaPlayer;
    private Boolean isPlaying = false;

    private float x, y, z, last_x, last_y, last_z;
    private long lastUpdate;

    //record times of useful shakes for test
    private int mTimes;
    //越小越灵敏
    private static final int SHAKE_THRESHOLD = 3000;

    private void initView() {
        textView1 = findViewById(R.id.text1);
        textView2 = findViewById(R.id.text2);
        textView3 = findViewById(R.id.text3);
    }


    //甩歌
    public void dragMusic() {
        if (!mediaPlayer.isPlaying()) {
            Log.e("Tag-", "播放");
            mediaPlayer.start();
        } else {
            mediaPlayer.pause();
        }
    }


    @SuppressLint("SetTextI18n")
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        textView1.setText("X=" + sensorEvent.values[0]);
        textView2.setText("Y=" + sensorEvent.values[1]);
        textView3.setText("Z=" + sensorEvent.values[2]);

        doOnSensorChangedJob(sensorEvent.sensor.getType(), sensorEvent.values);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    /**
     * 具体感应器改变操作
     *
     * @param sensor 传感器
     * @param values 传感器指
     */
    //甩歌算法
    private void doOnSensorChangedJob(int sensor, float[] values) {
        if (sensor == Sensor.TYPE_ACCELEROMETER) {
            long curTime = System.currentTimeMillis();
            //测100毫秒速度
            if ((curTime - lastUpdate) > 100) {
                long diffTime = (curTime - lastUpdate);
                x = values[SensorManager.DATA_X];
                y = values[SensorManager.DATA_Y];
                z = values[SensorManager.DATA_Z];
                float speed = Math.abs(x + y - last_x - last_y - last_z) / diffTime * 10000;

                Log.e("Tag-", "Speed:" + speed);
                if (speed > SHAKE_THRESHOLD) {
                    //检测到摇晃后执行的代码
                    Toast.makeText(this, "检测到甩歌", Toast.LENGTH_SHORT).show();
                    Log.e("Tag-", "甩出的幅度x:" + x + ",y:" + y + ",z:" + z);

                    //监听甩动
                    dragMusic();
                }
                last_x = x;
                last_y = y;
                last_z = z;
                lastUpdate = curTime;
            }
        }
    }


}
